package net.hvacapp.cmvrfforandroid.ui;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.mirror.android.common.util.StringUtils;

import net.hvacapp.cmvrfforandroid.R;
import net.hvacapp.cmvrfforandroid.http.HttpConstant;
import net.hvacapp.cmvrfforandroid.manager.AccountManager;
import net.hvacapp.cmvrfforandroid.manager.ProjectManager;
import net.hvacapp.cmvrfforandroid.ui.BaseActivity;
import net.hvacapp.cmvrfforandroid.ui.MainTabBarActivity;
import net.hvacapp.cmvrfforandroid.utils.AnnotationUtil;
import net.hvacapp.cmvrfforandroid.utils.UIHelper;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

@EActivity(R.layout.activity_webview)
public class WebviewActivity extends BaseActivity {

    @ViewById(R.id.webview)
    WebView mWebView;

    private String mUrl;
    private Dialog mDialog;

    /**
     * 页面加载完后执行
     */
    @AfterViews
    @Override
    protected void viewDidLoad() {
        super.viewDidLoad();
    }

    @Override
    public void initBoot() {
    }

    @SuppressLint({"JavascriptInterface", "SetJavaScriptEnabled"})
    @Override
    public void initData(Intent intent) {
        final String title = intent.getStringExtra("title");
        setupToolbar(0, title, null);
        mUrl = intent.getStringExtra("url");

        setButton1Info("分享", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UIHelper.shareFunctionWithUI(WebviewActivity.this, title, "", mUrl, getScreenBitmap());
            }
        });


        mWebView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);

        WebSettings webSettings = mWebView.getSettings();
        webSettings.setDefaultTextEncodingName("UTF-8");
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAppCacheEnabled(false);
        webSettings.setUseWideViewPort(true);//设定支持viewport
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setSupportZoom(true);//设定支持缩放

        // 让页面直接显示在webView客户端，用户不用再去选择浏览器显示。
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url); // 在当前的webview中跳转到新的url
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                mDialog.dismiss();
            }

        });
        mDialog = createProgressBarDialog(WebviewActivity.this,
                "加载中...");
        if (mDialog != null) {
            mDialog.show();
        }
        mWebView.loadUrl(mUrl);
    }


    /**
     * 页面名称
     */
    @Override
    public String getName() {
        return "";
    }


}
